package com.hazardSoft.eStore.models;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;

import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;

public class ModelManagerOffices {

	private String name;
	private String address;
	private byte[] image;
	private String imagePath;
	
	private int citySelected;
	private int departmentSelected;
	
	private String mime;
	private int render;
	
	public ModelManagerOffices()
	{
		render=0;
	
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public byte[] getImage() {
		return image;
	}
	public void setImage(byte[] image) {
		this.image = image;
	}
	public String getImagePath() {
		return imagePath;
	}
	public void setImagePath(String imagePath) {
		this.imagePath = imagePath;
	}
	public int getCitySelected() {
		return citySelected;
	}
	public void setCitySelected(int city) {
		this.citySelected = city;
	}
	public int getDepartmentSelected() {
		return departmentSelected;
	}
	public void setDepartmentSelected(int department) {
		this.departmentSelected = department;
	}	
	public String getMime() {
		return mime;
	}
	public void setMime(String mime) {
		this.mime = mime;
	}
	
	public int getRender() {
		return render;
	}

	public void setRender(int render) {
		this.render = render;
	}

	public void limpiar() {
		name="";
		address="";
		image=new byte[0];
		imagePath=null;
		mime="";

		citySelected=0;
		departmentSelected=1;

	}
	
	public void listener(UploadEvent event){		
        UploadItem item = event.getUploadItem();
        mimeType(item.getFileName());        
        try {
			FileInputStream inputStream=new FileInputStream(item.getFile());
			if(item.getFileSize()<800001)
			{
				image=new byte[item.getFileSize()];
				inputStream.read(image);
				render=1;
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }  
	public void paint(OutputStream stream, Object object) throws IOException {
	   if(Integer.parseInt(object.toString())==1)
	   {
		   render=0;
		   stream.write(image);		   
	   }
	   else
	   {
		   stream.write(0);
		   image=null;
	   }
    }
	
	public void mimeType(String fileName) {
		String name = fileName;
		int extDot = name.lastIndexOf('.');
		if(extDot > 0){
			String extension = name.substring(extDot +1);
			if("bmp".equals(extension)){
				mime="image/bmp";
			} else if("jpg".equals(extension)){
				mime="image/jpg";
			} else if("gif".equals(extension)){
				mime="image/gif";
			} else if("png".equals(extension)){
				mime="image/png";
			} else {
				mime = "image/unknown";
			}
		}
	}
}
